import {Vector3} from "./Vector3";

export class Box3 {
    min: Vector3 = new Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
    max: Vector3 = new Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
    
    constructor(vectors: Vector3[] = []) {
        this.addPoints(vectors);
    }

    addPoints(points: Vector3[]) {
        for (const point of points) {
            this.addPoint(point);
        }
    }
    
    addPoint(point: Vector3) {
        const {x, y, z} = point;
        this.min.x = Math.min(this.min.x, point.x);
        this.min.y = Math.min(this.min.y, point.y);
        this.min.z = Math.min(this.min.z, point.z);
        this.max.x = Math.max(this.max.x, point.x);
        this.max.y = Math.max(this.max.y, point.y);
        this.max.z = Math.max(this.max.z, point.z);
    }

    get size(): Vector3 {
        return this.max.subed(this.min)
    }
    get center() {
        return new Vector3((this.min.x + this.max.x) / 2, (this.min.y + this.max.y) / 2,(this.min.z + this.max.z) / 2);
    }
}